import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.radio模块已被废弃，替代模块为qturboframe.public_ui.h_ui.radio", DeprecationWarning)

from selenium.webdriver.common.by import By

from qturboframe.lib.ranorex.web_element.web_document import WebDocument
from qturboframe.lib.ranorex.web_element.web_element import WebElement
from qturboframe.lib.ranorex.web_element.input_tag import InputTag
from qturboframe.lib.ranorex.web_element.label_tag import LabelTag
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.check import CheckBasic
from ..ucf_base_element import BaseElement


class Radio(BaseElement, CheckBasic):
    """
    hui的单选按钮操作

    :Attributes:
     - elements: 单选按钮元素列表，list
    """

    def __init__(self, element_para, father=None, timeout=10000):
        if isinstance(element_para, list) and len(element_para) > 0:
            if isinstance(element_para[0], WebElement):
                self._elements = [InputTag(webelement.control) for webelement in element_para]
                self._driver = element_para[0].owner_document()
                self.timeout = timeout
            else:
                raise AttributeError('element_para参数不正确，需是WebElement的集合！')
        elif not isinstance(element_para, dict):
            raise AttributeError('element_para参数类型不正确！')
        else:
            if father is None:
                raise AttributeError('未定位元素，father参数不能为None！')
            if isinstance(father, WebDocument):
                self._driver = father
                self.timeout = timeout
            elif isinstance(father, WebElement):
                self._driver = father.owner_document()
                self.timeout = timeout
            else:
                raise AttributeError('father参数类型不正确！')
            key_list = ['XPATH', 'RANOREXPATH']
            element_by = ''
            element_value = ''
            assist = ''
            for key, value in element_para.items():
                if key in key_list:
                    element_by = key
                    element_value = value
                else:
                    assist += '{}:{},'.format(key, value)
            assist = assist.strip(',')
            self._elements = [InputTag(webelement.control) for webelement in
                              father.find_elements_by_attribute(element_by, element_value, assist, self.timeout)]

    @property
    def elements(self):
        '''
        获取单选按钮元素列表

        :Returns:
            返回单选按钮元素列表

        :Example:
         - 例: radio_element_list = radio.elements
        '''
        return self._elements

    def set_value(self, text):
        """
        hui的单选按钮控件选择

        :Args:
         - text: 选择项的文本

        :Example:
         - 例: radio.set_value('苹果')
        """
        for input_ele in self._elements:
            radio_label = LabelTag(input_ele.find_element_by_attribute(
                'xpath', './parent::span/parent::label', timeout=self.timeout).control)
            radio_text = radio_label.text
            if text == radio_text:
                input_ele.secure_click()
                break

    def is_default(self, value):
        """
        检查hui单选按钮默认值

        :Args:
         - value: 待检查的选项值

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: radio.is_default('苹果')
        """
        for input_ele in self._elements:
            radio_label = LabelTag(input_ele.find_element_by_attribute(
                'xpath', './parent::span/parent::label', timeout=self.timeout).control)
            radio_class = radio_label.get_attribute('class')
            radio_text = radio_label.text
            if 'h-radio-wrapper-checked' in radio_class:
                if value == radio_text:
                    return True
        else:
            logger.error('默认值不匹配！')
            return False

    def check_default(self, value):
        '''
        校验hui单选按钮默认值

        :Args:
         - value: 待检查的选项值

        :Raises:
            SeleniumError: 单选按钮的默认值检查不通过

        :Example:
         - 例: radio.check_default('苹果')
        '''
        if not self.is_default(value):
            raise CsClientError('单选按钮的默认值检查不通过！')